#if defined gangzones_inc
#endinput
#endif
#define gangzones_inc

#include <a_samp>
#include num_players

#if !defined MAX_ZONES
#define MAX_ZONES 20
#endif
#if !defined MAX_TEAMS
#define MAX_TEAMS 15
#endif
#if !defined RESET_OWNER_TIME
#define RESET_OWNER_TIME 60
#endif
#if !defined  MAX_TEAM_ATTACKS
#define  MAX_TEAM_ATTACKS 3
#endif
#if !defined GetTeam
#define GetTeam(%0) GetPlayerTeam(%0)
//#define GetTeam(%0) gTeam[%0]
#endif

#define mymin(%1,%2) (((%1)>(%2))?(%2):(%1))
#define mymax(%1,%2) (((%1)>(%2))?(%1):(%2))

//IsPosInRectangle(x,y,minx,miny,maxx,maxy);
#define IsPosInRectangle(%1,%2,%3,%4,%5,%6) ((((%1)>=(%3))&&((%1)<=(%5)))&&(((%2)>=(%4))&&((%2)<=(%6))))

new Float:gangzone_pos[MAX_ZONES][4];
new gangzone_team_id[MAX_ZONES][2];
new bool:gangzone_isvalid[MAX_ZONES];

new gangzone_teamwork[MAX_ZONES][MAX_TEAMS];
new gangzone_timer[MAX_ZONES];

new teamcolor[MAX_TEAMS];

forward gangzone_ResetOwner(gangzone, teamid);

stock CreateGangZoneEx(teamid, Float:x1, Float:y1, Float:x2, Float:y2)
{
	for(new i=0; i<MAX_ZONES; i++)
	if(!gangzone_isvalid[i])
	{
		if(teamid >=0 && teamid < MAX_TEAMS) gangzone_team_id[i][0] = teamid;
		gangzone_pos[i][0] = mymin(x1, x2);
		gangzone_pos[i][1] = mymin(y1, y2);
		gangzone_pos[i][2] = mymax(x1, x2);
		gangzone_pos[i][3] = mymax(y1, y2);
		gangzone_isvalid[i] = true;
		gangzone_team_id[i][1] = GangZoneCreate(gangzone_pos[i][0],gangzone_pos[i][1],gangzone_pos[i][2],gangzone_pos[i][3]);
		return i;
	}
	return(-1);
}

stock SetTeamColors(teamid, color, ...)
{
	new tmp;
	new anum = numargs();
	teamcolor[teamid] = color;
	for(new i = 0; i < anum; i+=2)
	{
		tmp = getarg(i, 0);
		if(tmp >= 0 && tmp < MAX_TEAMS)
			teamcolor[tmp] = getarg(i+1, 0);
	}
	return anum;
}

gng_onplayerconnect(playerid)
{
	for(new i=0; i<MAX_ZONES; i++) GangZoneShowForPlayer(playerid, \
		gangzone_team_id[i][1], teamcolor[gangzone_team_id[i][0]]);
	return true;
}

gng_onplayerdeath(playerid, killerid)
{
	new Float:kpos[3];
	new kteam = GetTeam(killerid);
	if(GetTeam(playerid) != kteam) // valid kill (not a teamkill)
	{
		GetPlayerPos(killerid, kpos[0], kpos[1], kpos[2]);
		for(new i=0; i<MAX_ZONES; i++)
		{
			if(IsPosInRectangle(kpos[0],kpos[1],  \
				gangzone_pos[i][0],gangzone_pos[i][1],gangzone_pos[i][2],gangzone_pos[i][3]))	// {killerid} killed {playerid} in gangzone {i}
			{
				if(kteam != gangzone_team_id[i][0] && GetTeam(playerid) == gangzone_team_id[i][0])
				{
					if(!gangzone_teamwork[i][kteam])
						GangZoneFlashForAll(gangzone_team_id[i][1], teamcolor[kteam]);
					gangzone_teamwork[i][kteam]++;
					KillTimer(gangzone_timer[i]);
					if(gangzone_teamwork[i][kteam] >= MAX_TEAM_ATTACKS)
					{
						for(new j=0; j<MAX_TEAMS; j++)
							gangzone_teamwork[i][j] = 0;
						gangzone_team_id[i][0] = kteam;
						GangZoneStopFlashForAll(gangzone_team_id[i][1]);
						GangZoneShowForAll(gangzone_team_id[i][1], teamcolor[kteam]);
						SendClientMessage(killerid, 0xffccccff, "Control restored, well done!");
						for(new k=0; k<MAX_PLAYERS; k++) if(IsPlayerConnected(k) && k!=killerid)
						{
							if(GetTeam(k)==kteam)
								SendClientMessage(k, 0xddddddff, "We've regained control, good work men!");
							else if(GetTeam(k)==GetTeam(playerid))
								SendClientMessage(k, 0xddddddff, "We've lost control of the outpost.");
						}
					}
					else gangzone_timer[i] = SetTimerEx("gangzone_ResetOwner", RESET_OWNER_TIME * 1000, false, "dd", i, kteam);
					printf("%d attacked gangzone[%d] for %d time", killerid, i, gangzone_teamwork[i][kteam]);
				}
				return true;
			}
		}
	}
	return true;
}


public gangzone_ResetOwner(gangzone, teamid)
{
	GangZoneStopFlashForAll(gangzone_team_id[gangzone][1]);
	gangzone_teamwork[gangzone][teamid] = 0;
	gangzone_timer[gangzone] = 0;
	return true;
}

